package com.cn.applet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cn.applet.service.CoreUserService;
import com.cn.applet.utils.vo.ResultResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Dai Zhengtao
 * 
 * @date 2018-05-17
 * 
 * */
@RestController
@Api(value = "Core User",description = "用户管理")
@RequestMapping("/appletservice/user")
public class CoreUserController {

	@Autowired
	private CoreUserService userService;
	
	
	@ApiOperation(value = "获取所有用户", notes = "获取所有用户", httpMethod = "GET")
	@RequestMapping("/find/all")
	public ResultResponse selectAll(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {

		return userService.selectAll(pageSize, pageIndex);
	}
	
	@ApiOperation(value = "根据编号获取用户信息", notes = "根据编号获取用户信息", httpMethod = "GET")
	@RequestMapping("/id")
	public ResultResponse getById(@ApiParam(value="用户编号",required=true) @RequestParam String userId) throws Exception{
		
		return userService.selectById(userId);
	}
	
	@ApiOperation(value = "增加用户", notes = "增加用户", httpMethod = "POST")
	@RequestMapping("/add")
	public ResultResponse addUser(@ApiParam(value="登录密码",required=true) @RequestParam String password,
			@ApiParam(value="电话",required=true) @RequestParam String phone,
			@ApiParam(value="用户名",required=true) @RequestParam String username,
			@ApiParam(value="邮箱",required=true) @RequestParam String email) throws Exception{
		
		return userService.addUser(password, phone, username, email);
	}
	
	@ApiOperation(value="修改指定用户", notes="修改指定用户",httpMethod="POST")
	@RequestMapping("/update")
	public ResultResponse updateUser(@ApiParam(value="需要修改的用户的编号", required = true) @RequestParam String userId,
			@ApiParam(value="新用户名",required=true) @RequestParam String username,
			@ApiParam(value="新电话号码",required=true) @RequestParam String phone,
			@ApiParam(value="新邮件",required=true) @RequestParam String email) throws Exception{
		
		return userService.updateUser(userId, username, phone, email);
	}
	
	@ApiOperation(value="删除用户",notes="删除用户",httpMethod="GET")
	@RequestMapping("/delete")
	public ResultResponse deleteUser(@ApiParam(value="用户ID",required=true) @RequestParam(value="userId",required=true) String userId) throws Exception{
		
		return userService.deleteUser(userId);
	}

}
